<?php


namespace LazadaApi\Services;


class LazadaOrderService extends BaseLazadaService
{
    const DOCUMENT_TYPE_INVOICE          = 'invoice';
    const DOCUMENT_TYPE_SHIPPING_LABEL   = 'shippingLabel';
    const DOCUMENT_TYPE_CARRIER_MANIFEST = 'carrierManifest';

    /**
     * 使用此API可以检索与订单相关的面单，包括发票和货运标签。
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=8&path=/order/document/get
     *
     * @param string $docType      Document types, including 'invoice', 'shippingLabel', or 'carrierManifest'.
     * @param array  $orderItemIds [279709, 279709]
     *
     * @return mixed
     * @throws \Exception
     */
    public function getDocument(string $docType, array $orderItemIds)
    {
        $this->addApiParam('doc_type', $docType);
        $this->addApiParam('order_item_ids', $orderItemIds);

        return $this->get('/order/document/get');
    }

    /**
     * 使用此API可以获取 SetStatusToCanceled API的其他错误原因。
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=8&path=/order/failure_reason/get
     */
    public function getFailureReasons()
    {
        return self::get('/order/failure_reason/get');
    }

    /**
     * 使用此API获取一个或多个订单的商品信息
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=8&path=/orders/items/get
     *
     * @param array $orderIds [42922, 32793] Max List Size: 100
     *
     * @return mixed
     * @throws \Exception
     */
    public function getMultipleOrderItems(array $orderIds)
    {
        $this->addApiParam('order_ids', $orderIds);

        return $this->get('/orders/items/get');
    }

    /**
     * 使用此API可以获取单个订单的商品列表
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=8&path=/order/get
     *
     * @param int $orderId
     *
     * @return mixed
     * @throws \Exception
     */
    public function getOrder(int $orderId)
    {
        $this->addApiParam('order_id', $orderId);

        return $this->get('/order/get');
    }

    /**
     * 使用此API获取订单的商品信息
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=8&path=/order/items/get
     *
     * @param int $orderId 31202
     *
     * @return mixed
     * @throws \Exception
     */
    public function getOrderItems(int $orderId)
    {
        $this->addApiParam('order_id', $orderId);

        return self::get('/order/items/get');
    }

    /**
     * 使用此API获取查询条件范围内订单的商品列表
     *
     * @see     https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=8&path=/orders/get
     *
     * @param array $queryParams
     *
     * @example $queryParams
     * [
     *      'created_before' => '2018-02-10T16:00:00+08:00',
     *      'created_after' => '2017-02-10T09:00:00+08:00',
     *      'status' => 'shipped', // When set, limits the returned set of orders to loose orders,
     *                             // which return only entries which fit the status provided.
     *                             // Possible values are unpaid, pending, canceled, ready_to_ship, delivered, returned,
     *                             // shipped and failed.
     *      'update_before' => '2017-02-10T09:00:00+08:00',
     *      'sort_direction' => 'DESC',
     *      'offset' => '0',
     *      'limit' => '10',
     *      'update_after' => '2017-02-10T09:00:00+08:00',
     *      'sort_by' => 'updated_at'
     * ]
     * @return mixed
     * @throws \Exception
     */
    public function getOrders(array $queryParams)
    {
        $this->setApiParams($queryParams);

        return self::get('/orders/get');
    }

    /**
     * 使用此API设置指定订单的发票编号
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=8&path=/order/invoice_number/set
     *
     * @param int    $orderItemId   123
     * @param string $invoiceNumber INV-20
     *
     * @return mixed
     * @throws \Exception
     */
    public function setInvoiceNumber(int $orderItemId, string $invoiceNumber)
    {
        $this->addApiParam('order_item_id', $orderItemId);
        $this->addApiParam('invoice_number', $invoiceNumber);

        return self::post('/order/invoice_number/set');
    }

    /**
     * 使用此API取消单个订单商品
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=8&path=/order/cancel
     *
     * @param int         $reasonId
     * @param int         $orderItemId
     * @param string|null $reasonDetail Out of stock [Not Required]
     *
     * @return mixed
     * @throws \Exception
     */
    public function setStatusToCanceled(int $reasonId, int $orderItemId, string $reasonDetail = null)
    {
        $this->addApiParam('reason_id', $reasonId);
        $this->addApiParam('order_item_id', $orderItemId);
        if (null !== $reasonDetail) {
            $this->addApiParam('reason_detail', $reasonDetail);
        }

        return $this->post('/order/cancel');
    }

    /**
     * 使用此API将订单商品标记为已打包
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=8&path=/order/pack
     *
     * @param string $shippingProvider Aramax
     * @param string $deliveryType     dropship
     * @param array  $orderItemIDs     [1530553,1830236]
     *
     * @return mixed
     * @throws \Exception
     */
    public function setStatusToPackedByMarketplace(string $shippingProvider, string $deliveryType, array $orderItemIDs)
    {
        $this->addApiParam('shipping_provider', $shippingProvider);
        $this->addApiParam('delivery_type', $deliveryType);
        $this->addApiParam('order_item_ids', $orderItemIDs);

        return $this->post('/order/pack');
    }

    /**
     * 使用此API将订单商品标记为可以发货
     *
     * @see https://open.lazada.com/doc/api.htm?spm=a2o9m.11193531.0.0.2c3d6bbe6GdZ0B#/api?cid=8&path=/order/rts
     *
     * @param string $deliveryType     dropship
     * @param array  $orderItemIDs     [1832590,1832592]
     * @param string $shipmentProvider Aramax
     * @param string $trackingNumber   12345678
     *
     * @return mixed
     * @throws \Exception
     */
    public function setStatusToReadyToShip(string $deliveryType, array $orderItemIDs, string $shipmentProvider, string $trackingNumber)
    {
        $this->addApiParam('delivery_type', $deliveryType);
        $this->addApiParam('order_item_ids', $orderItemIDs);
        $this->addApiParam('shipment_provider', $shipmentProvider);
        $this->addApiParam('tracking_number', $trackingNumber);

        return $this->post('/order/rts');
    }
}